<?php

require_once ("Common/InputValidationHandler.php");
require_once ("Common/LogHandler.php");
require_once ("view/NavigationView.php");


class InstallController {

	private $installHandler;
	private $installView;

	public function __construct(InstallHandler $a_installHandler, InstallView $a_installview) {
		$this->installHandler = $a_installHandler;
		$this->installView = $a_installview;
	}

	public function init() {

		//If we allready have a working installation
		if ($this -> installHandler -> isAllreadyInstalled() === true) {
			//Trying to uninstall?
			if ($this -> installView -> didUserClickUninstall() === TRUE) {
				$this -> installHandler -> uninstall();
				NavigationView::setHeadeLocation("Install.php");
			}

			return $this -> installView -> generateAllreadyInstalledOutPut();
		}
		//If user tried to send DBInfo
		if ($this -> installView -> didUserClickInstall() === true) {
			if ($this -> installView -> isValidForm() === true) {
				if ($this->installHandler-> createDBSettingsDocument($this -> installView -> getDataBaseInfoFromForm()) === true) {
					if ($this -> installHandler -> createAndPopulateTables() === true) {
						//Redirect to prevent double submit! This will trigger the connection test again
						//header("Location: Install.php");
						NavigationView::setHeadeLocation("Install.php");
					}
				}
			}
			$this -> installView -> generateFailMessage();
		}

		//No installation available, show form
		return $this -> installView -> renderInstallForm();
	}

}
